package com.cnzz.site1255174697.activity.taobao;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.cnzz.site1255174697.R;
import com.cqyanyu.yanyu.http.XCallback;
import com.cqyanyu.yanyu.http.XResultList;
import com.cqyanyu.yanyu.http.XResultPage;
import com.cqyanyu.yanyu.utils.StatusBarUtil;
import com.cqyanyu.yanyu.view.FontIconView;
import com.cqyanyu.yanyu.view.recyclerView.YRecyclerView;
import com.cqyanyu.yanyu.view.recyclerView.YRecyclerViewAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * 搜索的刷新Activity
 */
@ContentView(R.layout.activity_search)
public class SearchRecyclerActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener, YRecyclerView.OnLoadMoreListener, XCallback {

    public FontIconView btn_left;
    public FontIconView btn_right;
    public EditText etSearch;
    @ViewInject(R.id.mRecyclerView)
    public YRecyclerView mRecyclerView;
    public int page = 1;//当前页
    public int totalPages = 0;//页总数
    public YRecyclerViewAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        StatusBarUtil.setStatusBarColor(this, R.color.white);
        StatusBarUtil.StatusBarLightMode(this);
        initView();
    }

    /**
     * 初始化与绑定事件
     */
    private void initView() {
        btn_left = (FontIconView) findViewById(R.id.btn_left);
        btn_right = (FontIconView) findViewById(R.id.btn_right);
        etSearch = (EditText) findViewById(R.id.fi_title);
        mRecyclerView.setOnLoadMoreListener(this);
        mRecyclerView.setOnRefreshListener(this);
    }

    public void onBtnClick(View v) {
        //点击返回键
        if (v.getId() == R.id.btn_left) {
            finish();
        }
    }

    public void setBtn_rightText(CharSequence btn_rightText) {
        if (btn_right != null) {
            this.btn_right.setText(btn_rightText);
            this.btn_right.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置是否使用上拉加载状态，是否在加载
     *
     * @param loadMore
     */
    public void setLoadMore(boolean loadMore) {
        mRecyclerView.setLoadMore(loadMore);
    }

    /**
     * 设置setAdapter
     *
     * @param baseRecyclerViewAdapter
     */
    public void setAdapter(YRecyclerViewAdapter baseRecyclerViewAdapter) {
        mRecyclerView.setAdapter(baseRecyclerViewAdapter);
        mAdapter = baseRecyclerViewAdapter;
    }

    /**
     * 绑定指定数据类型的 viewHolder
     *
     * @param entityClass     数据实体类的class
     * @param viewholderClass viewHolder的实体类
     */
    public void bindItemHolder(Class entityClass, Class viewholderClass) {
        mRecyclerView.bindItemHolder(entityClass, viewholderClass);
        if (mAdapter == null) {
            mAdapter = mRecyclerView.getAdapter();
        }
    }

    /**
     * 加载数据
     */
    public void loadData() {
    }

    @Override
    public void onRefresh() {
        page = 1;
        loadData();
    }

    @Override
    public void onLoadMore() {
        page += 1;
        loadData();
    }
    /**
     * 加载完成设置数据
     * @param result
     */
    public void onSuccessSetData(Object result) {
        if (result instanceof XResultPage) {
            XResultPage xResultPage = (XResultPage) result;
            page = xResultPage.data.page;
            totalPages = xResultPage.data.totalPages;
            if (xResultPage.data.page == 1) {
                mAdapter.setData(xResultPage.data.data);
            } else {
                mAdapter.addData(xResultPage.data.data);
            }
        } else if (result instanceof XResultList) {
            XResultList xResultList = (XResultList) result;
            mAdapter.setData(xResultList.data);
        }
    }

    /**
     * 加载数据完成时处理
     */
    public void onFinishedEnd() {
        if (page < 2) {
            mRecyclerView.setRefreshing(false);
            mRecyclerView.setLoadMore(true);
        }
        if (page >= totalPages) {
            mRecyclerView.setLoadMore(false);
        }
        mRecyclerView.loadFinish();
    }

    @Override
    public void onSuccess(Object result) {
        onSuccessSetData(result);
    }

    @Override
    public void onFinished() {
        onFinishedEnd();
    }

}
